﻿namespace In66.Net.Share.Registrar
{
    public class ServiceInfo : IServiceInfo
    {
        private static ServiceInfo _instance = null;
        private static readonly object _lockObj = new();
        /// <summary>
        /// ServiceInfo
        /// </summary>
        private ServiceInfo()
        {
        }
        public string Id { get; private set; }
        public string Name { get; private set; }
        public string ServiceName { get; private set; }
        public string ShortName { get; private set; }
        public string CorsPolicy { get; set; }
        public string RelativeRootPath { get; private set; } = string.Empty;
        public string Version { get; private set; }
        public string Description { get; private set; }
        public Assembly StartAssembly { get; private set; }

        public static ServiceInfo CreateInstance(Assembly startAssembly)
        {
            if (_instance is not null)
            {
                return _instance;
            }

            lock (_lockObj)
            {
                if (_instance is not null)
                {
                    return _instance;
                }

                if (startAssembly is null)
                {
                    startAssembly = Assembly.GetEntryAssembly();
                }

                var description = startAssembly?.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description;
                var assemblyName = startAssembly?.GetName();
                var version = assemblyName?.Version;
                var fullName = assemblyName?.Name;
                var serviceName = fullName.ToLower();
                var ticks = DateTime.Now.GetTotalMilliseconds().ToLong();
                var ticksHex = Convert.ToString(ticks, 16);
                var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")?.ToLower();
                envName = "development";
                var serviceId = envName switch
                {
                    "development" => $"{serviceName}.dev.{ticksHex}",
                    "test" => $"{serviceName}.test.{ticksHex}",
                    "staging" => $"{serviceName}.stag.{ticksHex}",
                    "production" => $"{serviceName}.{ticksHex}",
                    _ => throw new NullReferenceException("ASPNETCORE_ENVIRONMENT")
                };
                var names = fullName.Split(".");
                _instance = new ServiceInfo
                {
                    Id = serviceId,
                    Name = fullName,
                    ServiceName = serviceName,
                    ShortName = $"{names[^2]}.{names[^1]}".ToLower(),
                    RelativeRootPath = $"{names[^2]}".ToLower(),
                    CorsPolicy = "default",
                    StartAssembly = startAssembly,
                    Description = description,
                    Version = $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"
                };
            }
            return _instance;
        }

        public static ServiceInfo GetInstance()
        {
            if (_instance is null)
            {
                throw new NullReferenceException(nameof(ServiceInfo));
            }

            return _instance;
        }
    }
}
